package ssm.integration.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * description: 数据库连接池的配置类-04
 * TODO：将 DataSource 的配置单独写到 DataSourceJavaConfig 配置类，避免和 MyBaits 配置组件冲突
 *
 * 1. @Configuration 声明配置类
 * 2. @PropertySource(“classpath:jdbc.properties”) 读取外部配置文件
 * 3. @Value("${}") 读取配置文件字段信息
 * 4. @Bean 将数据库连接池DataSource放入IOC容器
 */

@Configuration
@PropertySource("classpath:jdbc.properties")
public class DataSourceJavaConfig {

    @Value("${jdbc.user}")
    private String user;

    @Value("${jdbc.password}")
    private String password;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.driver}")
    private String driver;

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        return dataSource;
    }
}
